package com.mimikko.example;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

/*
* Author: ZYFDroid
* Description:
* 功能实现示例，可供参考，可以照抄。
* 建议启动的时候顺便开启防误触和位置锁定
*/


public class SleepCompanionActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
        super.onCreate(savedInstanceState);
        ActivityUtils.transparentSystemUI(this);
        setContentView(R.layout.sleep);
        bgView = findViewById(R.id.bg);
        textView = findViewById(R.id.txtCountdown);
        dimCoverView = findViewById(R.id.dimCover);
        try {
            float screenTimeoutSecond = Settings.System.getInt(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
            minuteBeforeReleaseKeep = (int) Math.ceil(screenTimeoutSecond / 1000 / 60) ;
            //获取自动熄屏的时间
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        SpUtils sp = SpUtils.on(this);
        totalMinute = sp.getSleepCountDown();
        screenDim = sp.isDimScreen();
        adaptOled = sp.isAdaptOLED();
        startImmersive();
        timerEndTime = System.currentTimeMillis() + 60 * 1000 * totalMinute + 30000L;
        hWnd.postDelayed(autoClose,300);
        Toast.makeText(this, "Tips: 快速按返回键退出", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
            startImmersive();
        }
    }
    long lastPress = 0;
    int quickPressed = 0;
    @Override
    public void onBackPressed() {
        if(System.currentTimeMillis() - lastPress > 500){
            quickPressed = 0;
        }
        else{
            quickPressed++;
            if(quickPressed>3){
                finish();
            }
        }
        lastPress = System.currentTimeMillis();
    }

    boolean adaptOled = true;
    boolean screenDim = true;

    @Override
    protected void onResume() {
        super.onResume();
        if (minute() <= 0){
            finish();
        }
    }

    View bgView;
    View dimCoverView;
    TextView textView;
    int minuteBeforeReleaseKeep = 5;
    int minute() {
        return (int) ((timerEndTime - System.currentTimeMillis()) / 60000);
    };
    int totalMinute = 45;

    long timerEndTime = 1145141919810L;

    int[] subpixoffBg = {R.drawable.amoled_bg1,R.drawable.amoled_bg2,R.drawable.amoled_bg3,R.drawable.amoled_bg4};
    Runnable autoClose = new Runnable() {
        @Override
        public void run() {
            if(adaptOled) {
                //像素覆盖，每四个像素熄灭一个，然后轮班
                bgView.setBackgroundResource(subpixoffBg[(minute() + 114514) % 4]);
            }
            if(screenDim){
                    dimCoverView.setAlpha(((float)totalMinute - (float)minute()) / (float)totalMinute * 0.7f);

            }
            textView.setText("00:"+(minute()>=10 ? "" : "0")+minute());
            //根据获取的屏幕自动熄灭时间提前取消保持屏幕长亮
            if(minute()<minuteBeforeReleaseKeep){
                textView.setKeepScreenOn(false);
            }
            if(minute() < 0){
                textView.setVisibility(View.GONE);
                return;
            }
            hWnd.postDelayed(this,60000);
        }
    };
    Handler hWnd = new Handler();
    @Override
    protected void onDestroy() {
        super.onDestroy();
        hWnd.removeCallbacks(autoClose);
    }

    public void startImmersive() {
        bgView.setPadding(0,0,0,0);
        ActivityUtils.startImmersive(this);
    }
}
